package org.tinaelle.client.gui.list;

import org.tinaelle.client.gui.grid.impl.AdCurrencyDataProvider;
import org.tinaelle.client.gui.grid.impl.AdLanguageDataProvider;
import org.tinaelle.client.gui.grid.impl.AdParameterDataProvider;
import org.tinaelle.client.gui.grid.impl.AdParameterValueDataProvider;
import org.tinaelle.client.gui.grid.impl.AdScreenDataProvider;
import org.tinaelle.client.gui.grid.impl.AdTableDataProvider;
import org.tinaelle.client.gui.grid.impl.AdTreeDataProvider;
import org.tinaelle.client.gui.grid.impl.AdTreeNodeDataProvider;
import org.tinaelle.client.gui.grid.impl.AdUserDataProvider;
import org.tinaelle.client.utils.Utilities;

import com.allen_sauer.gwt.log.client.Log;
import com.gwtext.client.core.UrlParam;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.event.StoreListenerAdapter;
import com.gwtext.client.widgets.form.ComboBox;

public class ListProvider {
	private static ListProvider	instance;

	private ListProvider() {
	}

	public static ListProvider getInstance() {
		if ( instance == null ) {
			instance = new ListProvider();
		}
		return instance;
	}

	public ComboBox getScreenCombox() {
		final AdScreenDataProvider dataProvider = new AdScreenDataProvider();
		final Store adScreenStore = new Store( dataProvider, new ArrayReader( dataProvider.getRecordDef() ), true );

		final ComboBox adScreenBox = new ComboBox( "Screen", "adScreenId" );
		adScreenBox.setValueField( "adScreenId" );
		adScreenBox.setDisplayField( "name" );
		adScreenBox.setMode( ComboBox.REMOTE );
		adScreenBox.setTriggerAction( ComboBox.ALL );
		adScreenBox.setEmptyText( "Select related screen" );
		adScreenBox.setLoadingText( "Searching..." );
		adScreenBox.setTypeAhead( true );
		adScreenBox.setMinChars( 1 );
		adScreenBox.setSelectOnFocus( true );
		adScreenBox.setForceSelection( true );

		// store.load();
		adScreenStore.addStoreListener( new StoreListenerAdapter() {
			@Override
			public void onLoad(final Store store, final Record[] records) {
				Log.debug( "adScreenStore::onLoad " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adScreenBox.getValue() ) ) {
					adScreenBox.setValue( adScreenBox.getValue() );
				}
			} // end onLoad()

			@Override
			public void onAdd(final Store store, final Record[] records, final int index) {
				Log.debug( "adScreenStore::onAdd " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adScreenBox.getValue() ) ) {
					adScreenBox.setValue( adScreenBox.getValue() );
				}
			}

			@Override
			public void onLoadException(final Throwable error) {
				Log.debug( "onLoadException() :: Store exception : " + error.getMessage(), error );
			} // end onLoadException()
		} // end new
		        );
		adScreenBox.setPageSize( 10 );
		adScreenBox.setStore( adScreenStore );

		return adScreenBox;
	} // end getScreenCombox()

	public ComboBox getTreeNodeCombox(final String label, final String valueField) {
		final AdTreeNodeDataProvider dataProvider = new AdTreeNodeDataProvider();
		final Store adTreeNodeStore = new Store( dataProvider, new ArrayReader( dataProvider.getRecordDef() ), true );

		final ComboBox adTreeNodeBox = new ComboBox( label, valueField );
		adTreeNodeBox.setDisplayField( "title" );
		adTreeNodeBox.setValueField( "adTreeNodeId" );
		adTreeNodeBox.setMode( ComboBox.REMOTE );
		adTreeNodeBox.setTriggerAction( ComboBox.ALL );
		adTreeNodeBox.setEmptyText( "Select parent node" );
		adTreeNodeBox.setLoadingText( "Searching..." );
		adTreeNodeBox.setTypeAhead( true );
		adTreeNodeBox.setMinChars( 1 );
		adTreeNodeBox.setSelectOnFocus( true );
		adTreeNodeBox.setForceSelection( true );

		adTreeNodeStore.addStoreListener( new StoreListenerAdapter() {
			@Override
			public void onLoad(final Store store, final Record[] records) {
				Log.debug( "adTreeNodeStore::onLoad " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adTreeNodeBox.getValue() ) ) {
					adTreeNodeBox.setValue( adTreeNodeBox.getValue() );
				}
			} // end onLoad()

			@Override
			public void onAdd(final Store store, final Record[] records, final int index) {
				Log.debug( "adTreeNodeStore::onAdd " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adTreeNodeBox.getValue() ) ) {
					adTreeNodeBox.setValue( adTreeNodeBox.getValue() );
				}
			}

			@Override
			public void onLoadException(final Throwable error) {
				Log.debug( "onLoadException() :: Store exception : " + error.getMessage(), error );
			} // end onLoadException()
		} // end new
		        );
		adTreeNodeBox.setPageSize( 10 );
		adTreeNodeBox.setStore( adTreeNodeStore );

		return adTreeNodeBox;
	} // end getTreeCombox()

	public ComboBox getTreeCombox() {
		return getTreeCombox( "adTreeId", "Tree" );
	}

	public ComboBox getTreeCombox(final String valueField, final String label) {
		final AdTreeDataProvider dataProvider = new AdTreeDataProvider();
		final Store adTreeStore = new Store( dataProvider, new ArrayReader( dataProvider.getRecordDef() ), true );

		final ComboBox adTreeBox = new ComboBox( label, valueField );
		adTreeBox.setDisplayField( "name" );
		adTreeBox.setValueField( valueField );
		adTreeBox.setMode( ComboBox.REMOTE );
		adTreeBox.setTriggerAction( ComboBox.ALL );
		adTreeBox.setEmptyText( "Select parent node" );
		adTreeBox.setLoadingText( "Searching..." );
		adTreeBox.setTypeAhead( true );
		adTreeBox.setMinChars( 1 );
		adTreeBox.setSelectOnFocus( true );
		adTreeBox.setForceSelection( true );

		adTreeStore.addStoreListener( new StoreListenerAdapter() {
			@Override
			public void onLoad(final Store store, final Record[] records) {
				Log.debug( "adTreeStore::onAdd " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adTreeBox.getValue() ) ) {
					adTreeBox.setValue( adTreeBox.getValue() );
				}
			} // end onLoad()

			@Override
			public void onAdd(final Store store, final Record[] records, final int index) {
				Log.debug( "adTreeStore::onAdd " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adTreeBox.getValue() ) ) {
					adTreeBox.setValue( adTreeBox.getValue() );
				}
			}

			@Override
			public void onLoadException(final Throwable error) {
				Log.debug( "onLoadException() :: Store exception : " + error.getMessage(), error );
			} // end onLoadException()
		} // end new
		        );
		adTreeBox.setPageSize( 10 );
		adTreeBox.setStore( adTreeStore );

		return adTreeBox;
	} // end getTreeCombox()

	/**
	 * DOCUMENT ME!
	 * 
	 * @return DOCUMENT ME!
	 */
	public ComboBox getTableCombox() {
		final AdTableDataProvider dataProvider = new AdTableDataProvider();
		final Store adTableStore = new Store( dataProvider, new ArrayReader( dataProvider.getRecordDef() ), true );

		final ComboBox adTableBox = new ComboBox( "Table", "adTableId" );
		adTableBox.setDisplayField( "name" );
		adTableBox.setValueField( "adTableId" );
		adTableBox.setMode( ComboBox.REMOTE );
		adTableBox.setTriggerAction( ComboBox.ALL );
		adTableBox.setEmptyText( "Select related table" );
		adTableBox.setLoadingText( "Searching..." );
		adTableBox.setTypeAhead( true );
		adTableBox.setMinChars( 1 );
		adTableBox.setSelectOnFocus( true );
		adTableBox.setForceSelection( true );

		adTableStore.addStoreListener( new StoreListenerAdapter() {
			@Override
			public void onLoad(final Store store, final Record[] records) {
				Log.debug( "adTableStore::onLoad " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adTableBox.getValue() ) ) {
					adTableBox.setValue( adTableBox.getValue() );
				}
			} // end onLoad()

			@Override
			public void onAdd(final Store store, final Record[] records, final int index) {
				Log.debug( "adTableStore::onAdd " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adTableBox.getValue() ) ) {
					adTableBox.setValue( adTableBox.getValue() );
				}
			}

			@Override
			public void onLoadException(final Throwable error) {
				Log.debug( "onLoadException() :: Store exception : " + error.getMessage(), error );
			} // end onLoadException()
		} // end new
		        );
		adTableBox.setPageSize( 10 );
		adTableBox.setStore( adTableStore );

		return adTableBox;
	} // end getTableCombox()

	public ComboBox getAdParameterValueComboBox(final String adParameterId, final String label, final String valueField) {
		final AdParameterValueDataProvider dataProvider = new AdParameterValueDataProvider();
		final Store adParameterValueStore = new Store( dataProvider, new ArrayReader( dataProvider.getRecordDef() ),
		        true );
		adParameterValueStore.setBaseParams( new UrlParam[] { new UrlParam( "adParameterId", adParameterId ) } );
		final ComboBox adParameterValueBox = new ComboBox( label, valueField );
		adParameterValueBox.setDisplayField( "name" );
		adParameterValueBox.setValueField( "adParameterValueId" );
		adParameterValueBox.setMode( ComboBox.REMOTE );
		adParameterValueBox.setTriggerAction( ComboBox.ALL );
		adParameterValueBox.setEmptyText( "Select related Parameter Value" );
		adParameterValueBox.setLoadingText( "Searching..." );
		adParameterValueBox.setTypeAhead( true );
		adParameterValueBox.setMinChars( 1 );
		adParameterValueBox.setSelectOnFocus( true );
		adParameterValueBox.setForceSelection( true );

		adParameterValueStore.addStoreListener( new StoreListenerAdapter() {
			@Override
			public void onLoad(final Store store, final Record[] records) {
				Log.debug( "adParameterValueStore::onLoad " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adParameterValueBox.getValue() ) ) {
					adParameterValueBox.setValue( adParameterValueBox.getValue() );
				}
			} // end onLoad()

			@Override
			public void onAdd(final Store store, final Record[] records, final int index) {
				Log.debug( "adParameterValueStore::onAdd " + records.length + " records loaded" );
				if ( records.length > 0 ) {
					Log.debug( records[ 0 ].getId() + " / " + records[ 0 ].getAsString( "adParameterValueId" ) );
				}
				if ( !Utilities.isBlankOrNull( adParameterValueBox.getValue() ) ) {
					adParameterValueBox.setValue( adParameterValueBox.getValue() );
				}
			}

			@Override
			public void onLoadException(final Throwable error) {
				Log.debug( "onLoadException() :: Store exception : " + error.getMessage(), error );
			} // end onLoadException()
		} // end new
		        );
		adParameterValueBox.setPageSize( 10 );
		adParameterValueBox.setStore( adParameterValueStore );

		return adParameterValueBox;
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @return DOCUMENT ME!
	 */
	public ComboBox getAdUserCombox() {
		return getAdUserCombox( "User", "adUserId" );
	}

	public ComboBox getAdUserCombox(final String label, final String valueField) {
		final AdUserDataProvider dataProvider = new AdUserDataProvider();
		final Store adUserStore = new Store( dataProvider, new ArrayReader( dataProvider.getRecordDef() ), true );

		final ComboBox adUserBox = new ComboBox( label, valueField );
		adUserBox.setDisplayField( "lastName" );
		adUserBox.setValueField( valueField );
		adUserBox.setMode( ComboBox.REMOTE );
		adUserBox.setTriggerAction( ComboBox.ALL );
		adUserBox.setEmptyText( "Select related User" );
		adUserBox.setLoadingText( "Searching..." );
		adUserBox.setTypeAhead( true );
		adUserBox.setMinChars( 1 );
		adUserBox.setSelectOnFocus( true );
		adUserBox.setForceSelection( true );

		adUserStore.addStoreListener( new StoreListenerAdapter() {
			@Override
			public void onLoad(final Store store, final Record[] records) {
				Log.debug( "adUserStore::onLoad " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adUserBox.getValue() ) ) {
					adUserBox.setValue( adUserBox.getValue() );
				}
			} // end onLoad()

			@Override
			public void onAdd(final Store store, final Record[] records, final int index) {
				Log.debug( "adUserStore::onAdd " + records.length + " records loaded" );
				if ( records.length > 0 ) {
					Log.debug( records[ 0 ].getId() + " / " + records[ 0 ].getAsString( "adUserId" ) );
				}
				if ( !Utilities.isBlankOrNull( adUserBox.getValue() ) ) {
					adUserBox.setValue( adUserBox.getValue() );
				}
			}

			@Override
			public void onLoadException(final Throwable error) {
				Log.debug( "onLoadException() :: Store exception : " + error.getMessage(), error );
			} // end onLoadException()
		} // end new
		        );
		adUserBox.setPageSize( 10 );
		adUserBox.setStore( adUserStore );

		return adUserBox;
	} // end getUserCombox()

	public ComboBox getAdParameterCombox() {
		return getAdParameterCombox( "Parameter", "adParameterId" );
	}

	public ComboBox getAdParameterCombox(final String label, final String valueField) {
		final AdParameterDataProvider dataProvider = new AdParameterDataProvider();
		final Store adParameterStore = new Store( dataProvider, new ArrayReader( dataProvider.getRecordDef() ), true );

		final ComboBox adParameterBox = new ComboBox( label, valueField );
		adParameterBox.setDisplayField( "name" );
		adParameterBox.setValueField( valueField );
		adParameterBox.setMode( ComboBox.REMOTE );
		adParameterBox.setTriggerAction( ComboBox.ALL );
		adParameterBox.setEmptyText( "Select related Parameter" );
		adParameterBox.setLoadingText( "Searching..." );
		adParameterBox.setTypeAhead( true );
		adParameterBox.setMinChars( 1 );
		adParameterBox.setSelectOnFocus( true );
		adParameterBox.setForceSelection( true );

		adParameterStore.addStoreListener( new StoreListenerAdapter() {
			@Override
			public void onLoad(final Store store, final Record[] records) {
				Log.debug( "adParameterStore::onLoad " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adParameterBox.getValue() ) ) {
					adParameterBox.setValue( adParameterBox.getValue() );
				}
			} // end onLoad()

			@Override
			public void onAdd(final Store store, final Record[] records, final int index) {
				Log.debug( "adParameterStore::onAdd " + records.length + " records loaded" );
				if ( records.length > 0 ) {
					Log.debug( records[ 0 ].getId() + " / " + records[ 0 ].getAsString( "adParameterId" ) );
				}
				if ( !Utilities.isBlankOrNull( adParameterBox.getValue() ) ) {
					adParameterBox.setValue( adParameterBox.getValue() );
				}
			}

			@Override
			public void onLoadException(final Throwable error) {
				Log.debug( "onLoadException() :: Store exception : " + error.getMessage(), error );
			} // end onLoadException()
		} // end new
		        );
		adParameterBox.setPageSize( 10 );
		adParameterBox.setStore( adParameterStore );

		return adParameterBox;
	} // end getParameterCombox()

	/**
	 * DOCUMENT ME!
	 * 
	 * @return DOCUMENT ME!
	 */
	public ComboBox getAdLanguageCombox() {
		return getAdLanguageCombox( "Language", "adLanguageId" );
	}

	public ComboBox getAdLanguageCombox(final String label, final String valueField) {
		final AdLanguageDataProvider dataProvider = new AdLanguageDataProvider();
		final Store adLanguageStore = new Store( dataProvider, new ArrayReader( dataProvider.getRecordDef() ), true );

		final ComboBox adLanguageBox = new ComboBox( label, valueField );
		adLanguageBox.setDisplayField( "name" );
		adLanguageBox.setValueField( valueField );
		adLanguageBox.setMode( ComboBox.REMOTE );
		adLanguageBox.setTriggerAction( ComboBox.ALL );
		adLanguageBox.setEmptyText( "Select related Language" );
		adLanguageBox.setLoadingText( "Searching..." );
		adLanguageBox.setTypeAhead( true );
		adLanguageBox.setMinChars( 1 );
		adLanguageBox.setSelectOnFocus( true );
		adLanguageBox.setForceSelection( true );

		adLanguageStore.addStoreListener( new StoreListenerAdapter() {
			@Override
			public void onLoad(final Store store, final Record[] records) {
				Log.debug( "adLanguageStore::onLoad " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adLanguageBox.getValue() ) ) {
					adLanguageBox.setValue( adLanguageBox.getValue() );
				}
			} // end onLoad()

			@Override
			public void onAdd(final Store store, final Record[] records, final int index) {
				Log.debug( "adLanguageStore::onAdd " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adLanguageBox.getValue() ) ) {
					adLanguageBox.setValue( adLanguageBox.getValue() );
				}
			}

			@Override
			public void onLoadException(final Throwable error) {
				Log.debug( "onLoadException() :: Store exception : " + error.getMessage(), error );
			} // end onLoadException()
		} // end new
		        );
		adLanguageBox.setPageSize( 10 );
		adLanguageBox.setStore( adLanguageStore );

		return adLanguageBox;
	} // end getLanguageCombox()

	public ComboBox getAdCurrencyCombox() {
		return getAdCurrencyCombox( "Currency", "adCurrencyId" );
	}

	public ComboBox getAdCurrencyCombox(final String label, final String valueField) {
		final AdCurrencyDataProvider dataProvider = new AdCurrencyDataProvider();
		final Store adCurrencyStore = new Store( dataProvider, new ArrayReader( dataProvider.getRecordDef() ), true );

		final ComboBox adCurrencyBox = new ComboBox( label, valueField );
		adCurrencyBox.setDisplayField( "isoCode" );
		adCurrencyBox.setValueField( valueField );
		adCurrencyBox.setMode( ComboBox.REMOTE );
		adCurrencyBox.setTriggerAction( ComboBox.ALL );
		adCurrencyBox.setEmptyText( "Select related Currency" );
		adCurrencyBox.setLoadingText( "Searching..." );
		adCurrencyBox.setTypeAhead( true );
		adCurrencyBox.setMinChars( 1 );
		adCurrencyBox.setSelectOnFocus( true );
		adCurrencyBox.setForceSelection( true );

		adCurrencyStore.addStoreListener( new StoreListenerAdapter() {
			@Override
			public void onLoad(final Store store, final Record[] records) {
				Log.debug( "adCurrencyStore::onLoad " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adCurrencyBox.getValue() ) ) {
					adCurrencyBox.setValue( adCurrencyBox.getValue() );
				}
			} // end onLoad()

			@Override
			public void onAdd(final Store store, final Record[] records, final int index) {
				Log.debug( "adCurrencyStore::onAdd " + records.length + " records loaded" );
				if ( !Utilities.isBlankOrNull( adCurrencyBox.getValue() ) ) {
					adCurrencyBox.setValue( adCurrencyBox.getValue() );
				}
			}

			@Override
			public void onLoadException(final Throwable error) {
				Log.debug( "onLoadException() :: Store exception : " + error.getMessage(), error );
			} // end onLoadException()
		} // end new
		        );
		adCurrencyBox.setPageSize( 10 );
		adCurrencyBox.setStore( adCurrencyStore );

		return adCurrencyBox;
	} // end getCurrencyCombox()
}
